{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2607e3a9",
   "metadata": {},
   "source": [
    "-----------\n",
    "* 20222/09/20/week05\n",
    "* ptyhon课堂笔记\n",
    "-----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1017cea7",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "> 1.有序的数据结构\n",
    "\n",
    "\n",
    "> 2.用户输入\n",
    "\n",
    "\n",
    "> 3.关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3c26ed9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_01_name = \"Marry\"\n",
    "\n",
    "# 思考：如果一个变量存放一个班同学的名字，改怎么存？\n",
    "# 思考1：处理数据会不会比较麻烦？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "de3151a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Marry,Alex,Jack,周小花\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = \"Marry,Alex,Jack,周小花\"\n",
    "print(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "88987b0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Alex'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考2：处理数据会不会比较麻烦？\n",
    "网三_name_list[6:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cbf57456",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考3：我想知道这个名单有多少个人名?\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f83db96b",
   "metadata": {},
   "source": [
    "## 1. 引入列表的概念\n",
    "* Lists might ocntain items of different types,but usually the items all have the same type\n",
    "\n",
    "> 注释：我们已经学习了 数值 和 字符，list课放数值和字符，但是通常要么一起放数值，要么一起放字符串。\n",
    "\n",
    "> 总结一些通用的有序数据结构的方法：\n",
    ">> 1.取值： 通过索引值取出对应的内容\n",
    ">> 2.切片： 通过索引值区间取出一些对应的内容\n",
    ">> 3.in 可以查看成员对象是否存在\n",
    "\n",
    "> 列表的一些方法\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "3e7017d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "9f6ec2f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Marry', 'Alex', 'Jack', '周小花'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = [\"Marry\",\"Alex\",\"Jack\",\"周小花\"]\n",
    "print(网三_name_list,type(网三_name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cc1cf90",
   "metadata": {},
   "source": [
    "### 1.1 列表的运算法则(Opration）\n",
    "* <font color=\"red\">运算法则的操作不会改变列表数据本身的存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "c9b7b725",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry'"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出 网三_name_list 中第一位同学的名字\n",
    "网三_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "22315f64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看网三_name_list 有多少位同学\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "b23526ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网三： Marry\n",
      "网三： Alex\n",
      "网三： Jack\n",
      "网三： 周小花\n"
     ]
    }
   ],
   "source": [
    "for i in 网三_name_list:\n",
    "    print(\"网三：\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "b6db5b49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in 可以查询数据是否存在\n",
    "\"Jack\" in 网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "d57f3a69",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于运算法则（in + 切片 取值 len（s） s。count）\n",
    "网三_name_list_转专业 = [\"Xiaoming\",\"Lihua\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "7335c5ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周小花', 'Xiaoming', 'Lihua']"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list + 网三_name_list_转专业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "7a1b38c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：一个list 可不可以出现 重复项?\n",
    "网一_age_list = [18,19,19,18,20,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "0020ccab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 19, 18, 20, 19]"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ca8a620",
   "metadata": {},
   "source": [
    "## 2.2 列表的方法（function）--对列表数据进行操作\n",
    "> 1.增      \n",
    "> 2.删     \n",
    "> 3.改     \n",
    "> 4.查      "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb5c822f",
   "metadata": {},
   "source": [
    "### append 增加，增加元素到末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "b5d36b9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list = [\"Marry\",\"Alex\",\"Jack\",\"周小花\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "39a26b82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周小花']"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "aaab1f2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"小花\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "01ec26ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周小花', '小花']"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "3da50ec2",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"小白\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "38f1f4b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周小花', '小花', '小白']"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "955b0ea7",
   "metadata": {},
   "source": [
    "# insert 增加元素到某个位置 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "9f1b9be9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "8ee98840",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.insert(1,\"李华\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "c3110de4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Alex', 'Jack', '周小花', '小花', '小白']"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "44c44033",
   "metadata": {},
   "outputs": [],
   "source": [
    "# remove 减"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "c11da781",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "0f0462d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.remove(\"Alex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "7ec68fb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Jack', '周小花', '小花', '小白']"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "c8f69919",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Jack', '周小花', '小花', '小白']"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove经常和 in 一起使用，先检查成员变量是否存在，然后再remove\n",
    "if \"李华\" in 网三_name_list:\n",
    "    网三_name_list.remove(\"李华\")\n",
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "ce9a64f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "3c02ddaa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "51c5aada",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Jack', '周小花', '小花', '小白']"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "e0bbad7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小白'"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "548da731",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "2da429a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '周小花', '小花']"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "0ccd5599",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践 ：请移除19 的元素，5分钟实践尝试\n",
    "age_list = [18,19,19,18,19,20,19,181,17]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "fbfc7aed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.count(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "259bc113",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 20, 19, 181, 17]"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "272ece23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 20, 181, 17]"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路1：\n",
    "删除的数量 = age_list.count(19)\n",
    "for i in range(删除的数量):\n",
    "    if 19 in age_list:\n",
    "        age_list.remove(19)\n",
    "    age_list\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "b70b95b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [18,19,19,18,19,20,19,181,17]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "0d05d7a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 19, 18, 19, 20, 19, 181, 17]"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "ae2f556a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 19\n",
      "3 19\n",
      "4 19\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 20, 181, 17]"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路2： pop(index),先决条件，知道值出现的位置\n",
    "\n",
    "for i,v in enumerate(age_list):\n",
    "    if v == 19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "052a7665",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
